{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Introduction to MCP\n",
    "\n",
    "This notebook file introduces the concept of Model Context Protocol using OpenAI Agents SDK framework."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Importing required libraries\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "from agents import Agent, Runner, trace\n",
    "from agents.mcp import MCPServerStdio\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv(override=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Using MCP in OpenAI Agents SDK\n",
    "\n",
    "1. Create a Client\n",
    "\n",
    "2. Have it spawn a server\n",
    "\n",
    "3. Collect the tools that the server can use\n",
    "\n",
    "Let's try the Fetch mcp-server that we looked at last week"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fetch_params = {\n",
    "    \"command\": \"uvx\", \n",
    "    \"args\": [\"mcp-server-fetch\"]\n",
    "    }\n",
    "\n",
    "async with MCPServerStdio(params=fetch_params, client_session_timeout_seconds=60) as server:\n",
    "    fetch_tools = await server.list_tools()\n",
    "\n",
    "fetch_tools"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Introducing another MCP server - this time Javascript based, with node"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# setting up path to node\n",
    "env = {\"PATH\": \"/usr/bin/node\" + os.environ[\"PATH\"]}\n",
    "playwright_params = {\n",
    "    \"command\": \"npx\",\n",
    "    \"args\": [ \"@playwright/mcp@latest\"], \n",
    "    \"env\": env\n",
    "    }\n",
    "\n",
    "async with MCPServerStdio(params=playwright_params, client_session_timeout_seconds=60) as server:\n",
    "    playwright_tools = await server.list_tools()\n",
    "\n",
    "playwright_tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sandbox_path = os.path.abspath(os.path.join(os.getcwd(), \"sandbox\"))\n",
    "env = {\"PATH\": \"/usr/bin/node\" + os.environ[\"PATH\"]}\n",
    "files_params = {\"command\": \"npx\", \"args\": [\"-y\", \"@modelcontextprotocol/server-filesystem\", sandbox_path], \"env\": env}\n",
    "\n",
    "async with MCPServerStdio(params=files_params,client_session_timeout_seconds=60) as server:\n",
    "    file_tools = await server.list_tools()\n",
    "\n",
    "file_tools"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Introducing Agent with Tools!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "env = {\"PATH\": \"/usr/bin/node\" + os.environ[\"PATH\"]}\n",
    "playwright_params = {\n",
    "    \"command\": \"npx\",\n",
    "    \"args\": [ \"@playwright/mcp@latest\"], \n",
    "    \"env\": env\n",
    "    }\n",
    "\n",
    "sandbox_path = os.path.abspath(os.path.join(os.getcwd(), \"sandbox\"))\n",
    "env = {\"PATH\": \"/usr/bin/node\" + os.environ[\"PATH\"]}\n",
    "files_params = {\n",
    "    \"command\": \"npx\", \n",
    "    \"args\": [\"-y\", \"@modelcontextprotocol/server-filesystem\", sandbox_path], \n",
    "    \"env\": env\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agents.extensions.models.litellm_model import LitellmModel\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "api_key = os.getenv(\"AMAZON_BEDROCK_API_KEY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_name = \"us.amazon.nova-micro-v1:0\"\n",
    "model=LitellmModel(model=model_name, api_key=api_key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "instructions = \"\"\"\n",
    "You browse the internet to accomplish your instructions.\n",
    "You are highly capable at browsing the internet independently to accomplish your task, \n",
    "including accepting all cookies and clicking 'not now' as\n",
    "appropriate to get to the content you need. If one website isn't fruitful, try another. \n",
    "Be persistent until you have solved your assignment,\n",
    "trying different options and sites as needed.\n",
    "\"\"\"\n",
    "\n",
    "async with MCPServerStdio(params=files_params, client_session_timeout_seconds=120) as mcp_server_files:\n",
    "    async with MCPServerStdio(params=playwright_params, client_session_timeout_seconds=120) as mcp_server_browser:\n",
    "        agent = Agent(\n",
    "            name=\"mcp_researcher\",\n",
    "            instructions=instructions,\n",
    "            model=model,\n",
    "            mcp_servers=[mcp_server_browser, mcp_server_files]\n",
    "        )\n",
    "\n",
    "        with trace(\"mcp_researcher\"):\n",
    "            result = await Runner.run(agent, \"Find the best way to present a LinkedIn profile to ensure recruiters are impressed and want to reach out. Present results as a markdown in linkedin.md\")\n",
    "            print(result.final_output)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
